#include "lsystem.h"
#include <string.h>

Lsystem_t readSystem() {
	Lsystem_t sysinfo;
	int i;
	char tmp;

	scanf("%c", &(sysinfo.move));
	scanf("%c", &tmp);
	scanf("%c", &(sysinfo.draw));

	scanf("%d", &(sysinfo.num_angles));
	scanf("%c", &tmp);
	sysinfo.angle_chars = malloc(sizeof(char)*sysinfo.num_angles);
	sysinfo.angles = malloc(sizeof(int)*sysinfo.num_angles);

	for(i = 0; i < sysinfo.num_angles; i++) {
		scanf("%c", &(sysinfo.angle_chars[i]));
		scanf("%d", &(sysinfo.angles[i]));
	}
	
	scanf("%d", &(sysinfo.num_rules));
	scanf("%c", &tmp);
	sysinfo.rule_predecessors = malloc(sizeof(char)*sysinfo.num_rules);
	sysinfo.rule_successors = malloc(sizeof(char*)*sysinfo.num_rules);

	for(i = 0; i < sysinfo.num_rules; i++) {
		scanf("%c", &(sysinfo.rule_predecessors[i]));
		scanf("%c", &tmp);
		sysinfo.rule_successors[i] = malloc(sizeof(char)*16);
		scanf("%s", sysinfo.rule_successors[i]);
		scanf("%c", &tmp);
	}

	sysinfo.start = malloc(sizeof(char)*8);
	scanf("%s", sysinfo.start);

	return sysinfo;
}

void printLsystem(Lsystem_t sysinfo) {
	int i;

	printf("move: %c draw: %c\n", sysinfo.move, sysinfo.draw);

	printf("# angles: %d\n", sysinfo.num_angles);
	for(i = 0; i < sysinfo.num_angles; i++) {
		printf("%c: %d\n", sysinfo.angle_chars[i], sysinfo.angles[i]);
	}

	printf("# rules: %d\n", sysinfo.num_rules);
	for(i = 0; i < sysinfo.num_rules; i++) {
		printf("%c: %s\n", sysinfo.rule_predecessors[i], sysinfo.rule_successors[i]);
	}

	printf("start: %s\n", sysinfo.start);
}

char* getSuccessor(Lsystem_t sysinfo, char predecessor) {
	int i;

	for(i = 0; i < sysinfo.num_rules; i++) {
		if(sysinfo.rule_predecessors[i] == predecessor) {
			return sysinfo.rule_successors[i];
		}
	}

	return "";
}
